layui.define(function (exports) {

    var a = layui.jquery,
        admin_map = a("#admin_map").val(),
        tc = '#GalleryTemplate',
        tcb = '#Gallery_',
        k = 'openMaterialDraggable',
        l = a('.' + k),
        layer = layui.layer,
        laytpl = layui.laytpl,
        element = layui.element,
        upload = layui.upload,
        T = {
            init: function () {
                //拖拽排序
                a.getScript("/static/admin/js/jquery-ui.js", function () {
                    a('.openMaterialDraggable').disableSelection();
                    a('.openMaterialDraggable').sortable({
                        axis: 'x',
                        cursor: "move",
                        update: function (event, ui) {
                            const parent = a(ui.item).parent(),
                                filed = parent.attr('data-filed'),
                                values = [];
                            l.find("img").each(function (index, img) {
                                values.push(a(img).attr("src"));
                            });
                            parent.find('#checkGallery_' + filed).val(values.join(','));
                        }
                    });
                });

                //选择图库
                a(".openGallery").click(function () {
                    var c = a(this).attr('data-c'), n = a(this).attr('data-n'), t = a(this).attr('data-t'),
                        v = a("#Gallery_" + n).find('#checkGallery_' + n).val(), h = 650,
                        w = window.frames.frameElement.id.includes('layui-layer-iframe'), l = w ? parent.layer : layer;
                    if (a(window).height() < h) h = (a(window).height() - 20);
                    v = typeof (v) !== 'undefined' ? v : '';
                    l.open({
                        type: 2,
                        area: ['1000px', h + 'px'],
                        fix: false,
                        maxmin: true,
                        shadeClose: true,
                        shade: 0.4,
                        anim: 2,
                        title: t == '1' ? '选择图片' : (t == '2' ? "选择视频" : '选择音频'),
                        content: '/' + admin_map + '/gallery/open.html?t=' + t + '&c=' + c + "&n=" + n + '&v=' + v,
                    });

                });
                l.on('mouseenter', '.del-wrap', function () {
                    a(this).find('.drop-down').show();
                }).on('mouseleave', '.del-wrap', function () {
                    a(this).find('.drop-down').hide();
                }).on('click', '.drop-del', function () {
                    var v = a(this).attr('data-val'), p = a(this).parents('.' + k).find('.drop-value');
                    var val = p.val().split(',');
                    var index = val.indexOf(v);
                    index > -1 ? val.splice(index, 1) : '';
                    p.val(val.join(','))
                    a(this).parents('.material-preview').remove();
                });


                //直接上传
                upload.render({
                    elem: '.uploadGalleryNil',
                    url: '/' + admin_map + '/gallery/upload.html',
                    multiple: true,
                    data: {
                        type: 1,
                    },

                    before: function (obj) {

                        var ccc = Object.values(obj.getChooseFiles()).length;
                        var box = a(this.item), n = box.attr('data-n'), c = box.attr('data-c'),
                            v = a("#checkGallery_" + n).val() ? a("#checkGallery_" + n).val().split(',') : [];

                        if(ccc + v.length > c){
                            layer.msg('最多上传' + c + '张图片');
                        }

                        if (ccc > c) {
                            layer.msg('最多上传' + c + '张图片');
                            return false;
                        }

                        if (c > 1) {
                            if (v.length >= c) {
                                layer.msg('最多上传' + c + '张图片');
                                return false;
                            }
                        }
                        element.progress('progress', '0%')
                        layer.msg('' + $('.progress').html() + '', {
                            shade: 0.4,
                            time: 0
                        });
                    },
                    progress: function (n, elem, res, index) {
                        element.progress('progress', n + '%');
                    },
                    done: function (data, index, upload) {
                        var box = a(this.item),
                            n = box.attr('data-n'),
                            ty = box.attr('data-t'),
                            c = parseInt(box.attr('data-c')),
                            v = a("#checkGallery_" + n).val() ? a("#checkGallery_" + n).val().split(',') : [],
                            t = a(tc + ty).html();

                        c === 1 ? v = [data.data] : v.push(data.data);
                        laytpl(t).render({data: v, name: n}, function (html) {
                            a(tcb + n).html(html);
                        });
                        layer.closeAll();
                    },
                    error: function () {
                        layer.closeAll();
                        layer.msg('上传失败');
                    }
                });

                renderGallery = function (i, u, n, ty) {
                    var t = a(tc + ty).html(), w = window.frames.frameElement.id.includes('layui-layer-iframe'),
                        l = w ? parent.layer : layer;
                    laytpl(t).render({data: u, name: n}, function (html) {
                        a(tcb + n).html(html);
                    });
                    l.close(l.index)
                }
            }
        }


    exports('gallery', T);
});